package com.genesis.yunnanji;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.genesis.yunnanji.activity.StartAdver;
import com.genesis.yunnanji.bean.App_Start;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

/**
 * 启动页
 * Created by Administrator on 2017/6/6.
 */

public class SplashActivity extends AppCompatActivity {
    private ImageView imageView;
    private String adverImgUrl;
    private String adverLinkUrl;
    private GenesisUtils genesisUtils;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        genesisUtils = GenesisUtils.getIntance();
        context = this;
        initView(savedInstanceState);
        setUpView();
    }

    protected void initView(Bundle savedInstanceState) {
        imageView = (ImageView) findViewById(R.id.iv_splash);
        StatusBarUtil.setTransparentForImageView(this, imageView);

    }


    protected void setUpView() {
        getAdver();

        try {
            PackageInfo info = getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(getApplicationContext().getPackageName(), 0);
            String versionName = info.versionName;
            if (!versionName.equals(genesisUtils.getVersionName())) {
                genesisUtils.setIsFirst(true);
                genesisUtils.setVersionName(versionName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getAdver() {
        OkHttpUtils
                .get()
                .url(GenesisApiConfig.HOST + GenesisApiConfig.BANNER)
                .addParams("code", "app_start")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(context, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }, 2000);
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        Gson gson = new Gson();
                        App_Start bannerBean = gson.fromJson(response, App_Start.class);
                        if (genesisUtils.isFirst()) {
                            Intent intent = new Intent(context, WelcomeActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            if (bannerBean.getStatus() == 200) {//获取成功
                                if (bannerBean.getResult().getList().size() != 0) {
                                    adverImgUrl = bannerBean.getResult().getList().get(0).getAdvert_img();
                                    adverLinkUrl = bannerBean.getResult().getList().get(0).getTarget_link();
                                    Intent intent = new Intent(context, StartAdver.class);
                                    intent.putExtra("advert_img", adverImgUrl);
                                    intent.putExtra("target_link", adverLinkUrl);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Intent intent = new Intent(context, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            } else {
                                new Timer().schedule(new TimerTask() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent(context, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }, 2000);
                            }
                        }
                    }
                });

    }

}
